// 通知相关API调用 - 与后端NotificationApi接口保持一致

import request from '@/utils/request'
import type {
  Notification,
  NotificationPageResponse,
  OrderStatusDTO,
  PriceChangeDTO,
  SecurityAlertDTO,
  SystemAlertDTO
} from '@/types/notification'

// 分页查询通知列表
export const getNotificationList = (pageNo: number, pageSize: number, notification?: Partial<Notification>) => {
  const params = { pageNo, pageSize, ...notification }
  return request.get<NotificationPageResponse>('/api/notifications/findAll', { params })
}

// 根据ID查询通知
export const getNotificationById = (id: number) => {
  return request.get<Notification>('/api/notifications/findById', { params: { id } })
}

// 保存通知
export const saveNotification = (notification: Notification) => {
  return request.post<boolean>('/api/notifications/save', notification)
}

// 更新通知
export const updateNotification = (notification: Notification) => {
  return request.put<boolean>('/api/notifications/update', notification)
}

// 批量删除通知
export const deleteNotifications = (ids: number[]) => {
  return request.delete<number>('/api/notifications/deleteByIds', { data: ids })
}

// 订单状态变更通知
export const sendOrderStatusChangeNotification = (dto: OrderStatusDTO) => {
  return request.post<string>('/api/notifications/orderStatusChange', dto)
}

// 价格变更通知
export const sendPriceChangeNotification = (dto: PriceChangeDTO) => {
  return request.post<string>('/api/notifications/priceChange', dto)
}

// 安全警报通知
export const sendSecurityAlertNotification = (dto: SecurityAlertDTO) => {
  return request.post<string>('/api/notifications/securityAlert', dto)
}

// 系统通知
export const sendSystemAlertNotification = (dto: SystemAlertDTO) => {
  return request.post<string>('/api/notifications/systemAlert', dto)
} 

// 获取用户通知列表（用于头部通知弹窗）
export const getUserNotifications = (pageNo: number = 1, pageSize: number = 10, userId?: number) => {
  return request.get<NotificationPageResponse>('/api/notifications/user', {
    params: { pageNo, pageSize, userId }
  })
}

// 获取用户未读通知数量
export const getUnreadNotificationCount = (userId?: number) => {
  return request.get<number>('/api/notifications/unread/count', {
    params: { userId }
  })
}

// 标记通知为已读
export const markNotificationAsRead = (notificationId: number) => {
  return request.put<boolean>(`/api/notifications/${notificationId}/read`)
}

// 标记所有通知为已读
export const markAllNotificationsAsRead = (userId?: number) => {
  return request.put<boolean>('/api/notifications/readAll', null, {
    params: { userId }
  })
} 